<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区角类型">
        <el-select
          v-model="queryParams.cornerType"
          placeholder="请选择区角类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in cornerOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="阶层">
        <el-select
          v-model="queryParams.step"
          placeholder="请选择阶层"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="queryParams.title" placeholder="请输入标题" size="small" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="cornerGameList"
    >
      <el-table-column label="区角类型" align="center" prop="cornerType" :formatter="cornerTypeFormat" />
      <el-table-column label="阶层" align="center" prop="step" >
        <template slot-scope="scope">
          <div>
            <span v-html="scope.row.step == '1' ? '第一阶' : scope.row.step == '2' ? '第二阶' :'第三阶'">
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="图片" align="center" prop="gameImg">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.gameImg" alt="" style="width: 50px;height:50px;">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="游戏描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改区角材料对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form
        :rules="rules"
        :model="infoValue"
        ref="infoValue"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="区角类型" prop="cornerType">
          <el-select
            v-model="infoValue.cornerType"
            placeholder="请选择区角类型"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in cornerOption"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="阶层" prop="step">
          <el-select
            v-model="infoValue.step"
            placeholder="请选择阶层"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="infoValue.title" placeholder="请输入标题" size="small" />
        </el-form-item>
        <el-form-item label="游戏描述" prop="description">
          <el-input v-model="infoValue.description" placeholder="请输入游戏标题" size="small" type="textarea"/>
        </el-form-item>
        <el-form-item label="上传图片：" prop="gameImg">
          <imageUpload v-model="infoValue.gameImg" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCornerGameList,
  addCornerGame,
  updateCornerGame,
  batchCornerGame,
} from "@/api/qu/material";
import { getToken } from "@/utils/auth";

export default {
  name: "Teacher",
  components: {  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      typeOptions: [
        {
          dictValue: 1,
          dictLabel: "第一阶",
        },
        {
          dictValue: 2,
          dictLabel: "第二阶",
        },
        {
          dictValue: 3,
          dictLabel: "第三阶",
        },
      ],
      cornerOption: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      infoValue: {},
      // 表单校验
      rules: {
        cornerType: [{ required: true, message: "区角类型不能为空", trigger: "change" }],
        step: [{ required: true, message: "阶层不能为空", trigger: "change" }],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        gameImg: [
          { required: true, message: "图片不能为空", trigger: "blur" },
        ],
      },
      graduateSchool: "",
      nameList: [],
      cornerGameList:[]
    };
  },
  async created() {
    await this.getList();
    this.getDicts("corner_type").then((response) => {
      this.cornerOption = response.data;
    });
  },
  methods: {
    cornerTypeFormat(row, column) {
      return this.selectDictLabel(this.cornerOption, row.cornerType);
    },
    /** 查询区角材料列表 */
    getList() {
      this.loading = true;
      // const params = JSON.stringify(this.queryParams);
      getCornerGameList(this.queryParams)
        .then((response) => {
          this.cornerGameList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.infoValue = {};
      this.resetForm("infoValue");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.reset();
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加区角材料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row,'row');
      this.open = true;
      this.title = "修改区角材料";
      this.infoValue = row
      this.infoValue.cornerType = row.cornerType+''
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["infoValue"].validate((valid) => {
        if (valid) {
          if (this.infoValue.id != null) {
            updateCornerGame(this.infoValue).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCornerGame(this.infoValue).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除选中的教师数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return batchCornerGame(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
  },
};
</script>
